package org.javascud.extensions.cxf.testfailover;

import org.apache.cxf.clustering.FailoverFeature;
import org.apache.cxf.clustering.RandomStrategy;
import org.apache.cxf.feature.AbstractFeature;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.javascud.extensions.cxf.service.Hello;

import java.util.ArrayList;
import java.util.List;

public class HelloServiceFailOverClient
{
    public static void main(String[] args)
    {
        String helloFirst = "http://localhost:8080/service/Hello";
        String helloSecond = "http://localhost:8081/service/Hello";
        String helloThird = "http://localhost:8082/service/Hello";
        String helloFour = "http://localhost:8083/service/Hello";

        List<String> serviceList = new ArrayList<String>();
        serviceList.add(helloFirst);
        serviceList.add(helloSecond);
        serviceList.add(helloThird);
        //serviceList.add(helloFour);

        RandomStrategy strategy = new RandomStrategy();
        strategy.setAlternateAddresses(serviceList);

        FailoverFeature ff = new FailoverFeature();
        ff.setStrategy(strategy);

        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

        List<AbstractFeature> features = new ArrayList<AbstractFeature>();
        features.add(ff);

        factory.setFeatures(features);
        factory.initFeatures();

        factory.setServiceClass(Hello.class);
        //factory.setAddress("http://localhost:8080/service/Hello");

        Hello client = (Hello) factory.create();
        String result = client.sayHello("felix");
        System.out.println("result is: " + result);
    }
}
